package com.nkdx.ws.service.impl;

import com.nkdx.ws.dto.UserDto;
import com.nkdx.ws.enums.CodeEnum;
import com.nkdx.ws.eto.UserEto;
import com.nkdx.ws.exception.DataException;
import com.nkdx.ws.mapper.UserMapper;
import com.nkdx.ws.service.UserService;
import com.nkdx.ws.util.Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 *   用户 Service
 * @author ZZW
 * @date 2022/3/3 4:51 下午
 * @param
 * @return
 */

@Service
public class UserServiceImpl implements UserService {

    private UserMapper userMapper;
    private HttpServletRequest request;

    @Autowired
    public UserServiceImpl (UserMapper userMapper,HttpServletRequest request){
        this.userMapper = userMapper;
        this.request = request;
    }

    @Override
    public List<UserDto> findAllUsers() {
        return userMapper.findAllUsers();
    }

    @Override
    public void updateUserLastLoginTime(Integer id, Date lastLoginTime) {
        if (id != null){
            userMapper.updateUserLastLoginTime(id,lastLoginTime);
        }
    }

    @Override
    public UserDto findUserById(Integer id) {
        if (id != null && id > 0){
            return userMapper.findUserById(id);
        }
        return null;
    }

    @Override
    public UserDto update(UserEto eto) {
        if (eto != null && eto.getId() != null){
            eto.setUpdateTime(new Date());
            if (StringUtils.isNotBlank(eto.getPassword())){
                eto.setPassword(Utils.MD5(eto.getPassword()));
            }
            userMapper.update(eto);
            UserDto user = userMapper.findUserById(eto.getId());
            UserDto loginUser = (UserDto)request.getSession().getAttribute("user");
            if (eto.getId().equals(loginUser.getId())){
                request.getSession().setAttribute("user",user);
            }
            return user;
        }
        return null;
    }

    @Override
    public UserDto insert(UserEto eto) {
        Date date = new Date();
        eto.setCreateTime(date);
        eto.setUpdateTime(date);
        eto.setLastLoginTime(date);
        eto.setPassword(Utils.MD5(eto.getPassword()));
        int insert = userMapper.insertSelective(eto);
        if (insert>0){
            return userMapper.findUserById(eto.getId());
        }else {
            return null;
        }
    }

    /**
     *
     *  校验用户名是否存在,
     * @author ZZW
     * @date 2022/3/4 3:44 下午
     * @param
     * @return
     */
    @Override
    public UserDto verificationName(String name) {
        if (StringUtils.isBlank(name)){
            throw new DataException(CodeEnum.COM_MISS_PARAM_ERROR);
        }
        UserDto user = userMapper.findUserByName(name);
        if (user != null){
            throw new DataException(CodeEnum.ACCOUNT_ALREADY_EXISTS);
        }
        return user;
    }

    @Override
    public UserDto getLoginUser() {
        UserDto user = (UserDto) request.getSession().getAttribute("user");
        if (user == null){
            throw new DataException(CodeEnum.ERROR_APP_ID_NOT_EXISTS);
        }
        return userMapper.findUserById(user.getId());
    }
}
